package org.kfive.nova.algorithm.sort;

/**
 * Created with IntelliJ IDEA.
 * User: admin
 * Date: 14-5-13
 * Time: 上午10:21
 * To change this template use File | Settings | File Templates.
 */
public class InsertSort {

    public static void insertSort(int [] source){
        if(source!=null&&source.length>0){
            for(int i=1;i<source.length;i++){
                for(int j=i;j>0&&source[j]<source[j-1];j--){
                        int temp = source[j-1];
                        source[j-1] = source[j];
                        source[j] = temp;
                }
            }
        }
    }

    public static void insertSort1(int [] source){
        if(source!=null&&source.length>0){
            for(int i=1;i<source.length;i++){
                for(int j=i;j>0;j--){
                    if(source[j]<source[j-1]){
                        int temp = source[j-1];
                        source[j-1] = source[j];
                        source[j] = temp;
                    }
                }
            }
        }
    }
    public static void main(String [] args){
        int [] source = {8,3,6,8,1,2,0,4,1,56,78,123,45,98};
        insertSort1(source);
        for (int i=0;i<source.length;i++){
            System.out.println(source[i]);
        }
    }
}
